package com.zr.task.dialog;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import androidx.annotation.NonNull;

import com.zh.baselibrary.utils.TTSUtils;
import com.zh.model.task.home.ValidateBean;
import com.zr.task.R;
import com.zr.task.view.MultiRadioGroup;

import java.util.Arrays;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class ChooseSubjectDialog extends Dialog {
    @BindView(R.id.radio_group)
    MultiRadioGroup radioGroup;
    @BindView(R.id.bt_submit)
    Button btSubmit;
    private String subjectName;
    private String radioCode;
    private OnClickListener onClickListener;
    public ChooseSubjectDialog(@NonNull Context context, int themeResId, String radioCode,OnClickListener onClickListener) {
        super(context,themeResId);
        this.radioCode = radioCode;
        this.onClickListener = onClickListener;
    }

    public void setRadioCode(String radioCode) {
        this.radioCode = radioCode;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_subject);
        ButterKnife.bind(this);
        //设置不可取消，点击其他区域不能取消
        setCancelable(false);
        setCanceledOnTouchOutside(false);
        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                RadioButton button = findViewById(checkedId);
                subjectName = button.getText().toString();
                btSubmit.setEnabled(true);
            }
        });

    }
    @OnClick({R.id.bt_submit,R.id.iv_close})
    public void onClick(View view) {
        if(view.getId() == R.id.bt_submit) {
            ValidateBean validateBean = new ValidateBean();
            validateBean.setRadioCode(radioCode);
            validateBean.setSubjectName(subjectName);
            onClickListener.onClick(validateBean);
            cancel();
        }else {
            onClickListener.onCancel();
            cancel();
        }

    }

    @Override
    public void show() {
        super.show();
        TTSUtils.getInstance().speak(getContext().getString(R.string.choose_relation_subject));
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM,
                WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
    }

    @Override
    public void cancel() {
        super.cancel();
        getWindow().clearFlags(WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
    }

    public interface OnClickListener {
        void onClick( ValidateBean validateBean );
        void onCancel();
    }
}
